﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuadRemote.Core
{
    class FlightSession
    {
        private int storageID;
        private long dateCreated;

        private bool realtime;
        public bool IsRealtime
        {
            get
            {
                return realtime;
            }
        }

        ConfigDefinition config;
        public ConfigDefinition Config
        {
            get
            {
                return config;
            }
        }

        LogStore log;
        public LogStore Log
        {
            get
            {
                return log;
            }
        }

        private static FlightSession currentSession;
        public static FlightSession current()
        {
            return currentSession;
        }

        public FlightSession(String filename)
        {
            StorageReader reader = new StorageReader(filename);

            try
            {
                StorageMarker marker = reader.readMarker();

                String name = reader.ReadString();

                if (marker == StorageMarker.LIST_BEGIN && name == "V1")
                {
                    marker = reader.readMarker();
                    while (marker != StorageMarker.LIST_END)
                    {
                        name = reader.ReadString();

                        if (name == "LogStore")
                        {
                            log = new LogStore(reader);
                        }
                        else if (name == "Config")
                        {
                            // ...
                        }
                        else
                        {
                            reader.ignore(marker);
                        }
                        marker = reader.readMarker();
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }

            reader.Close();

            currentSession = this;
        }

        public FlightSession()
        {
            config = new ConfigDefinition();
            log = new LogStore();
            dateCreated = DateTime.Now.Ticks;
            realtime = true;

            Bluetooth.instance().commandParsers[(int)CommandId.CMD_LOG_SEND_DEFINITIONS] = incomingLogDefinition;
            Bluetooth.instance().commandParsers[(int)CommandId.CMD_CONFIG_VALUE] = incomingConfigValue;

            currentSession = this;
        }

        public void resetLogs()
        {
            log.resetData();
        }

        public void incomingLogDefinition(CommandIn packet)
        {
            Console.WriteLine("Log definition incoming");
            if (packet.packet.Length > 0)
                log.addDefinition(packet.packet, 0);
        }

        public void incomingConfigValue(CommandIn packet)
        {
            config.incomingConfigValue(packet);
        }

        public void save(String filename)
        {
            StorageWriter writer = new StorageWriter(filename);
            writer.startList("V1");

            log.save(writer);

            writer.endList();
            writer.Close();
        }
    }
}
